import { Component } from '@angular/core';
import { IonicPage, NavController, NavParams } from 'ionic-angular';
import { StorageProvider } from "../../providers/storage/storage";
import { ValidateProvider } from "../../providers/validate/validate";
import { HttpServicesProvider } from "../../providers/http-services/http-services";
import { UtilsProvider } from "../../providers/utils/utils";
// import {isNumeric} from "rxjs/util/isNumeric";

@IonicPage({
    segment: 'member/info/step2',
    defaultHistory: ['MemberPage']
})
@Component({
    selector: 'page-register2',
    templateUrl: 'register2.html',
})
export class Register2Page {
    regData = {
        recent_photo: 0,
        address: 0,
        spare_mobile_number: 0,
        other_photo: [],
        wechat: 0,
        qq: 0
    };
    step = 1;
    wxStep: any;
    QQStep: any;
    telStep: any;
    constructor(
        public navCtrl: NavController,
        public navParams: NavParams,
        public storage: StorageProvider,
        public validate: ValidateProvider,
        public httpService: HttpServicesProvider,
        public util: UtilsProvider
    ) {
    }

    //判断用户是否登录
    ionViewCanEnter() {
        return this.validate.checkLogin();
    }

    ionViewWillEnter() {
        this.getData();
    }

    getData() {
        let tokenInfo = JSON.parse(this.storage.get('tokenInfo'));
        let token = tokenInfo['token'];
        let apiUrl = "/user_info/get_register_info/page/2/token/" + token;
        this.httpService.requestData(apiUrl, response => {
            this.regData = response['data'];
            this.telStep = this.regData.spare_mobile_number == 0 ? false : true;
            this.wxStep = this.regData.wechat == 0 ? false : true;
            this.QQStep = this.regData.qq == 0 ? false : true;
            this.step = response['step'];
            this.regData.spare_mobile_number == 0 ? '' : this.regData.spare_mobile_number;
            this.regData.wechat == 0 ? '' : this.regData.wechat;
            this.regData.qq == 0 ? '' : this.regData.qq;
        })
    }

    uploadImage(type, e) {
        let file = e.target.files[0];

        if (file == undefined) {
            return false;
        }
        if (file.type.toString().toLowerCase().indexOf("image") < 0) {
            this.util.presentAlert("亲，这不是图片哦~");
            return false;
        }
        let data = new FormData();

        data.append('image', file);
        data.append('type', 'moments');
        data.append('water', 'true');


        this.httpService.uploadImage('/user/upload_file', data, result => {
            switch (type) {
                case 'recent_photo':
                    this.regData.recent_photo = result.img_path;
                    break;
                case 'other_photo_0':
                    this.regData.other_photo[0] = result.img_path;
                    break;
                case 'other_photo_1':
                    this.regData.other_photo[1] = result.img_path;
                    break;
                case 'other_photo_2':
                    this.regData.other_photo[2] = result.img_path;
                    break;
                case 'other_photo_3':
                    this.regData.other_photo[3] = result.img_path;
                    break;
                case 'other_photo_4':
                    this.regData.other_photo[4] = result.img_path;
                    break;
                case 'other_photo_5':
                    this.regData.other_photo[5] = result.img_path;
                    break;
                default:
                    return false;
            }
        });
    }

    //提交第一步注册信息
    doPost() {
        // if(this.regData.recent_photo == 0 || this.regData.recent_photo == null){
        //     this.util.presentAlert('请上传您的近照');
        //     return false;
        // }

        if (this.regData.address == 0 || this.regData.address == null) {
            this.util.presentAlert('详细地址不能为空');
            return false;
        }

        // if(this.regData.spare_mobile_number == 0 || !isNumeric(this.regData.spare_mobile_number)){
        //     this.util.presentAlert('备用电话不能为空或格式有误');
        //     return false;
        // }

        if (this.regData) {
            this.util.presentConfirm('当前资料提交后不可修改，请确认所填信息是否属实', () => {
                let tokenInfo = JSON.parse(this.storage.get('tokenInfo'));
                let token = tokenInfo['token'];
                let apiUrl = "/user/reg_two/token/" + token;
                this.httpService.requestPostData(apiUrl, this.regData, response => {
                    if (response['code'] == 200) {
                        this.navCtrl.push('RegisterSuccessPage');
                    }
                })
            });
        } else {
            this.navCtrl.popToRoot();
        }
    }


}
